﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace WebProyectoSIETE
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["usuario"] != null)
            {
                SqlConnection conexion = null;
                try
                {
                    conexion = new SqlConnection(ConfigurationManager.ConnectionStrings["cadenaConexion"].ConnectionString);

                    conexion.Open();
                    SqlCommand comando = new SqlCommand("SELECT [nombreMision], [idMision], [mapaMision], [inicioMision], [descripcionMision], [puntosMision], [dificultadMision], [objetivoMision], [imagenMapa] FROM [Mision],[Mapa] WHERE [mapaMision] = [idMapa] ORDER BY [nombreMision]", conexion);
                    SqlDataReader dr = comando.ExecuteReader();
                    Button bt;
                    Image img;


                    PlaceHolder1.Controls.Add(new LiteralControl("<div class=\"Mision\">"));
                    while (dr.Read())
                    {
                        PlaceHolder1.Controls.Add(new LiteralControl("<div class=\"objetoTaberna\"> <div class=\"datosObjetoTaberna\"> <div class=\"tipoObjetoTaberna\">" + dr[0].ToString()));
                        PlaceHolder1.Controls.Add(new LiteralControl("</div><div class=\"nombreObjetoTaberna\"> Dificultad: " + dr[6].ToString() + "</div><div class=\"detallesObjetoTaberna\">"));
                        PlaceHolder1.Controls.Add(new LiteralControl("Puntos: " + dr[5].ToString() + "<br //> Descripcion: " + dr[4].ToString() + "<br //> <div class=\"imagenObjetoTaberna\">"));

                        img = new Image();
                        bt = new Button();

                        img.ImageUrl = "~/img/" + dr[8] + ".png";
                        img.ID = "img" + dr[0].ToString();
                        img.Height = 150;
                        img.Width = 150;
                        img.AlternateText = dr[2].ToString();
                        PlaceHolder1.Controls.Add(img);
                        PlaceHolder1.Controls.Add(new LiteralControl("</div>"));
                        PlaceHolder1.Controls.Add(new LiteralControl("</div>"));
                        PlaceHolder1.Controls.Add(new LiteralControl("</div>"));

                        bt.ID = dr[1].ToString();
                        bt.Text = "Aceptar Mision: " + dr[0].ToString();
                        bt.Visible = true;
                        bt.Click += new EventHandler(bt_Click);
                        PlaceHolder1.Controls.Add(bt);
                        int a = 5;
                        PlaceHolder1.Controls.Add(new LiteralControl("</div>"));

                    }
                    PlaceHolder1.Controls.Add(new LiteralControl("</div>"));
                }
                catch (Exception ex)
                {


                }
                finally
                {

                    conexion.Close();
                }
            }
            else
            {
                Session["noRegistrado"] = "a";
                Response.Redirect("registroUsuario.aspx");
            }
        }
       

        protected void bt_Click(object sender, EventArgs e)
        {
            // Como sabemos que quien lanza el evento es un Button, lo parseamos y lo recogemos en una variable
            Button bt = (Button)sender;

            // Ahora podemos hacer lo que creamos conveniente con el Button. Ej:
            Session["idMision"] = bt.ID;
            Response.Redirect("Mapa.aspx");
        }

    }
}
